/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * WebMvcConfig.java
 * appbasic
 * 2014年5月6日下午2:37:28
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.config;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.interceptor.ProxyInterceptor;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@PropertySource("classpath:/mvc.properties")
@ComponentScan(basePackages="org.jackysoft.controller",includeFilters=@Filter(Controller.class))
public class WebMvcConfig extends DelegatingWebMvcConfiguration  {
	
	static final Log logger = LogFactory.getLog(WebMvcConfig.class);
	@Inject
	Environment env;

	@PostConstruct
	public void init(){
	   String[] actpros =  env.getActiveProfiles();	 
	   for(String p : actpros){
		  if("test".equalsIgnoreCase(p)){
			  logger.info("I'm in test enviroment of mvc");
			  super.setServletContext(new MockServletContext());
		  }
	   }
	   
	}
	
	
	
	static final int cacheped = 31556926;
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/attachment/**")
				.addResourceLocations("/attachment/").setCachePeriod(cacheped);
		registry.addResourceHandler("/css/**").addResourceLocations("/css/")
				.setCachePeriod(cacheped);
		registry.addResourceHandler("/scripts/**")
				.addResourceLocations("/scripts/").setCachePeriod(cacheped);
		registry.addResourceHandler("/images/**")
				.addResourceLocations("/images/").setCachePeriod(cacheped);
		super.addResourceHandlers(registry);
	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/mvc/").setViewName("index");
		super.addViewControllers(registry);
	}

	@Override
	public void configureContentNegotiation(
			ContentNegotiationConfigurer configurer) {
		configurer.favorPathExtension(false).favorParameter(true);
		configurer.defaultContentType(MediaType.TEXT_HTML);
		super.configureContentNegotiation(configurer);
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new ProxyInterceptor());
		super.addInterceptors(registry);
	}
	

	@Bean
	public ViewResolver viewResolver(){
		InternalResourceViewResolver bean = new InternalResourceViewResolver();
		bean.setViewClass(org.springframework.web.servlet.view.JstlView.class);
		bean.setPrefix("/spring/");
		bean.setSuffix(".jsp");
		
		return bean;
	}	
	
	@Bean
	public MultipartResolver multipartResolver(){
		StandardServletMultipartResolver bean = new StandardServletMultipartResolver();
		return bean;
	}
	
	
	
	@Bean public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator(){
		AnnotationAwareAspectJAutoProxyCreator bean = new AnnotationAwareAspectJAutoProxyCreator();
		
		return bean;
	}
	
	
	
}
